package bjtu.annotation.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import bjtu.annotation.entity.Label;
import bjtu.annotation.entity.Wechat;
import bjtu.annotation.entity.WechatClassMap;

@Repository
@SuppressWarnings("unchecked")
public class WechatDaoImpl extends BaseDao implements WechatDao {

	public List<Wechat> selectWechatById(String id) {
		DetachedCriteria dc = DetachedCriteria.forClass(Wechat.class);
		dc.add(Restrictions.eq("id", Integer.parseInt(id)));
		return (List<Wechat>) this.getHibernateTemplate().findByCriteria(dc);
	}

	public void updateWechat(Wechat chat) {
		this.getHibernateTemplate().saveOrUpdate(chat);
	}

	@Override
	public List<Label> selectLabelById(String labelid) {
		// TODO Auto-generated method stub
		DetachedCriteria dc = DetachedCriteria.forClass(Label.class);
		dc.add(Restrictions.eq("id", Integer.parseInt(labelid)));
		return (List<Label>) this.getHibernateTemplate().findByCriteria(dc);
	}

	@Override
	public void addWechatClassMap(WechatClassMap wcm) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().saveOrUpdate(wcm);
	}

	@Override
	public List<Integer> selectExistMaxTimes(String labelid, int wechatid) {
		Session session = this.currentSession();
		try {
			String sql = "select max(times) from WechatClassMap where classid=" + labelid + " and wechatid=" + wechatid;
			return (List<Integer>) session.createQuery(sql).list();
		} catch (HibernateException e) {
			e.printStackTrace();
		}

		return null;
	}

}
